<?php
class Rakuten {

	//バージョン
	const API_VERSION = "2010-09-15";
	// 自分のディベロッパーID
	private $DEVELOPER_ID = "feaeec38b8bc37411b14de274b1d9480";
	//自分のアフィリエイトID
	private $AFFILIATE_ID = "0a30aaaf.afb9b1e2.0a30aab0.82880378";

	// APIのURL
	private $API_BASE_URL   = "http://api.rakuten.co.jp/rws/3.0/json";

	//オペレーション名
	private $OPERATION      = array(
		"ItemSearch" => array(
			"operation" => 'ItemSearch',
			'callBack' => '',
			'keyword' => '',
			'version' => self::API_VERSION,
			'shopCode' => '',
			'genreId' => '',
			'hits' => '',
			'page' => '',
			'sort' => '',
			'minPrice' => '',
			'maxPrice' => '',
			'availability' => '',
			'field' => '',
			'carrier' => '',
			'imageFlag' => '',
			'orFlag' => '',
			'NGKeyword' => '',
			'genreInformationFlag' => 1,
			'purchaseType' => '',
			'shipOverseasFlag' => '',
			'shipOverseasArea' => '',
			'asurakuFlag' => '',
			'asurakuArea' => '',
			'pointRateFlag' => '',
			'pointRate' => '',
			'postageFlag' => '',
			'creditCardFlag' => ''
		),
		'ItemCodeSearch' => array (
			"operation" => 'ItemCodeSearch',
			'itemCode' => '',
			'version' =>  '2010-08-05',
			'carrier' => ''
		),
		'BooksBookSearch' => array (
			"operation" => 'BooksBookSearch',
			'title' => '',
			'version' =>  '2011-12-01',
			'author' => '',
			'publisherName' => '',
			'isbn'=>'',
			'sort' => ''

		)
	);



	private $api_url        = "";

	//APIへのパラメタの連想配列
	private $api_params          = array();

	public function Rakuten () {
	}

	public function set($k,$v) {
		$this->api_params[$k] = $v;
	}

	public function setOperation ($k)
	{
		$this->api_params = $this->OPERATION[$k];
	}

	public function getProduct () {
		$this->api_url = sprintf("%s?developerId=%s&affiliateId=%s",$this->API_BASE_URL,$this->DEVELOPER_ID,$this->AFFILIATE_ID);
		$api_url = $this->api_url;
		$api_params = $this->api_params;
		reset($api_params);
		if($api_params['operation'] == "")
			$api_params['operation'] = "ItemSearch";
		while(list ($key, $val) = each($api_params) ){
		    if(isset($_REQUEST[$key]) && $_REQUEST[$key] != ""){
		        // リクエストパラメタにあれば、APIへのURLに追加
		        $api_url = sprintf("%s&%s=%s",$api_url, $key, urlencode($_REQUEST[$key]));
		        $api_params[$key] = $_REQUEST[$key];
		    }else if(isset($api_params[$key]) && $api_params[$key] != ""){
		        // パラメタにあれば、APIへのURLに追加
		        $api_url = sprintf("%s&%s=%s",$api_url, $key, urlencode($api_params[$key]));
		    }
		}
//		echo $api_url;
		$contents = file_get_contents($api_url);
		return $contents;
	}
}
